package activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.igexin.sdk.PushManager;
import com.laoxinwen.app.R;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import common.BaseActivity;
import common.MyApp;
import common.MyTheme;
import common.UserConfig;
import util.ToastUtil;
import util.bitmap.FileUtil;

/**
 * Created by ilioili on 2015/5/21.
 */
public class SettingsActivity extends BaseActivity {
    @id(R.id.tv_cache_size)
    private TextView tvCacheSize;

    @id(R.id.theme_spinner)
    private Spinner themeSpinner;

    private boolean firstSelectTheme;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        loadView();
        if (UserConfig.isPushOn()) {
            f(R.id.bt_push_switch).setSelected(true);
        } else {
            f(R.id.bt_push_switch).setSelected(false);
        }
        if (UserConfig.isNoPic()) {
            f(R.id.bt_pic_switch).setSelected(true);
        } else {
            f(R.id.bt_pic_switch).setSelected(false);
        }
        tvCacheSize.setText(getCacheSize());
        //主题
        final List<MyTheme> totalThemes = new ArrayList<MyTheme>( MyTheme.getTotalThemes() );
        firstSelectTheme = true;
        //去除夜间模式
        totalThemes.remove(0);

        themeSpinner.setAdapter(new ArrayAdapter<MyTheme>(this, R.layout.theme_spinner_item, R.id.theme_text, totalThemes) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                MyTheme theme = getItem(position);
                view.findViewById(R.id.theme_color).setBackgroundColor(theme.getMainColor());
                return view;
            }

            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                return getView(position, convertView, parent);
            }
        });
        themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                MyTheme theme = totalThemes.get(position);
                if (!firstSelectTheme && theme != MyApp.getMyTheme()) {
                    UserConfig.setNightMode(false);
                    UserConfig.setMainTheme(theme.getId());
                    MyApp.switchTheme(theme.getId());
                    restartSelf();
                }
                firstSelectTheme = false;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        themeSpinner.setSelection(totalThemes.indexOf( MyApp.getMyTheme()));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_clear_cache:
                File[] files = FileUtil.getFileDir().listFiles();
                for (File file : files) {
                    if (file.isFile()) {
                        file.delete();
                    }
                }
                files = FileUtil.getCacheDir().listFiles();
                for (File file : files) {
                    if (file.isFile()) {
                        file.delete();
                    }
                }
                ToastUtil.showShortToast(this, "已清除所有缓存数据");
                tvCacheSize.setText(getCacheSize());
                break;
            case R.id.bt_push_switch:
                if (UserConfig.isPushOn()) {
                    PushManager.getInstance().turnOffPush(getApplicationContext());
                    UserConfig.setIsPushOn(false);
                    view.setSelected(false);
                } else {
                    PushManager.getInstance().turnOnPush(getApplicationContext());
                    UserConfig.setIsPushOn(true);
                    view.setSelected(true);
                }
                break;
            case R.id.bt_pic_switch:
                if (UserConfig.isNoPic()) {
                    UserConfig.setIsNoPic(false);
                    view.setSelected(false);
                } else {
                    UserConfig.setIsNoPic(true);
                    view.setSelected(true);
                }
                break;
        }
    }

    public String getCacheSize() {
        long total = 0;
        File[] files = FileUtil.getFileDir().listFiles();
        for (File file : files) {
            if (file.isFile()) {
                total += file.length();
            }
        }
        files = FileUtil.getCacheDir().listFiles();
        for (File file : files) {
            if (file.isFile()) {
                total += file.length();
            }
        }
        if (total < 1024 * 1024) {
            return total / 1024 + "KB";
        } else {
            return total * 100 / 1024 / 1024 / 100f + "MB";
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("设置页");
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("设置页");
    }
}
